# -*- coding: utf-8 -*-
import cv2
import numpy as np
src = cv2.imread("car.png")
cv2.namedWindow("input", cv2.WINDOW_AUTOSIZE)
cv2.imshow("input", src)

hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0,43,46])
high_hsv = np.array([10,255,255])
mask = cv2.inRange(hsv,lowerb=low_hsv,upperb=high_hsv)
mask=cv2.flip(mask,flipCode=1)

cv2.imshow("test",mask)
cv2.waitKey(0)
cv2.destroyAllWindows()


rows,cols,channels = src.shape

src=cv2.resize(src,None,fx=0.5,fy=0.5)
rows,cols,channels = src.shape


hsv=cv2.cvtColor(src,cv2.COLOR_BGR2HSV)
lower_blue=np.array([78,43,46])
upper_blue=np.array([124,255,255])
mask = cv2.inRange(hsv, lower_blue, upper_blue)


s = mask[30:200, 50:250] #y,x




cv2.rectangle(src,(50,30),(250,200),(0,0,255),3)
cv2.imshow('res',src)

cv2.imshow('ss',s)

x,y= s.shape

bk = 0
wt = 0

for i in range(x):
    for j in range(y):
        if s[i,j]==0:
            bk+=1
        else:
            wt+=1
rate1 = wt/(x*y)
rate2 = bk/(x*y)

print("白色占比:", round(rate1*100,2),'%')
print("黑色占比:", round(rate2*100,2),'%')

cv2.waitKey(0)